package com.itguigu.fruit.impl;

import com.itguigu.fruit.dao.FruitDao;
import com.itguigu.fruit.pojo.Fruit;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FruitDaoImpl implements FruitDao {


    @Override
    public void addFruit(Fruit fruit) {
        Connection conn = null;
        PreparedStatement psmt = null;

        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.链接数据库
            conn = DriverManager.getConnection(URL, USER, PWD);

//            3.写SQL语句
            String sql = "insert into t_fruit values(null,?,?,?,?)";
//            4.psmt
            psmt = conn.prepareStatement(sql);
            //set

            psmt.setString(1, fruit.getFname());
            psmt.setInt(2, fruit.getPrice());
            psmt.setInt(3, fruit.getFcount());
            psmt.setString(4, fruit.getRemark());
            psmt.executeUpdate();

            //释放资源
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

    }

    @Override
    public void delFruit(String fname) {
        Connection conn = null;
        PreparedStatement psmt = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.链接数据库
            conn = DriverManager.getConnection(URL, USER, PWD);

//            3.写SQL语句
            String sql = "delete  from t_fruit where fname=?";
//            4.psmt
            psmt = conn.prepareStatement(sql);
            //set

            psmt.setString(1, fname);
//            psmt.setInt(2,fruit.getPrice());
//            psmt.setInt(3,fruit.getFcount());
//            psmt.setString(4,fruit.getRemark());
            psmt.executeUpdate();

            //释放资源
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    public void updateFruit(Fruit fruit) {
        Connection conn = null;
        PreparedStatement psmt = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.链接数据库
            conn = DriverManager.getConnection(URL, USER, PWD);

//            3.写SQL语句
            String sql = "update t_fruit set fcount=? where fname=?";
//            4.psmt
            psmt = conn.prepareStatement(sql);
            //set
            psmt.setInt(1, fruit.getFcount());
            psmt.setString(2, fruit.getFname());
//            psmt.setInt(2,fruit.getPrice());
//
//            psmt.setString(4,fruit.getRemark());
            psmt.executeUpdate();

            //释放资源
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {

                psmt.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    public List<Fruit> getFruitList() {
        List<Fruit> fruitList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.链接数据库
            conn = DriverManager.getConnection(URL, USER, PWD);

//            3.写SQL语句
            String sql = "select * from t_fruit";
//            4.psmt
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            while (rs.next()) {
                //整理数据类型
                Integer fid = rs.getInt(1);
                String fname = rs.getString(2);
                Integer price = rs.getInt(3);
                Integer fcount = rs.getInt(4);
                String remark = rs.getString("remark");

                //输出
                Fruit fruit = new Fruit(fid, fname, price, fcount, remark);
                fruitList.add(fruit);


//                System.out.println(fid+','+fname+','+price+','+fcount+','+remark);
            }
            return fruitList;

            //释放资源
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                rs.close();
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
//        return null;
    }

    @Override
    public Fruit getFruitByFname(String fname) {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
//         =null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.链接数据库
            conn = DriverManager.getConnection(URL, USER, PWD);

//            3.写SQL语句
            String sql = "select * from t_fruit where fname=?";
//            4.psmt
            psmt = conn.prepareStatement(sql);
            //set
//            psmt.setInt(1,fruit.getFcount());
            psmt.setString(1, fname);
            rs = psmt.executeQuery();
            if (rs.next()) {
                //整理数据类型
                Integer fid = rs.getInt(1);
//                String fname = rs.getString(2);
                Integer price = rs.getInt(3);
                Integer fcount = rs.getInt(4);
                String remark = rs.getString("remark");
                return new Fruit(fid, fname, price, fcount, remark);
//                 fruit;
                //输出
//                System.out.println(fid+','+fname+','+price+','+fcount+','+remark);
            }
//            psmt.setInt(2,fruit.getPrice());
//
//            psmt.setString(4,fruit.getRemark());


            //释放资源
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {

                rs.close();
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        return null;
    }
}
